<%@page import="multiface.awwadco.jpa.Utente"%>
<%@page import="java.util.List"%>
<%@page import="javax.persistence.Query"%>
<%@page import="multiface.awwadco.jpa.File"%>
<%@page import="multiface.awwadco.jpa.Prodotto"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page import="multiface.awwadco.jpa.PersistenceManager"%>
<%
    Utente u = (Utente) session.getAttribute("admin");
    if(u==null){
        response.sendRedirect(multiface.web.util.ServletUtils.getBaseUrl(request) + "/admin/index.jsp?err=y");
    }
    String pids = request.getParameter("pid");
%>

<script type="text/javascript">
    $(document).ready(function() {
        $('#uploadify').uploadify({
            'uploader': '../engine/plugin/uploadify/uploadify.swf',
            'script': '../admin/center/addFiles.jsp?pid=<%=pids%>',
            'folder': '/uploads',
            'cancelImg': '../engine/plugin/uploadify/cancel.png',
            'multi': true,
            'sizeLimit': 1048576*2,
            'auto' :true,
            'onComplete'  : function(event, ID, fileObj, response, data) {
                $(".images").append("<div id=\"file_"+jQuery.trim(response)+"\" onclick=\"javascript:preview("+jQuery.trim(response)+")\"><img src=\"../GetImageServlet?th=y&fid="+jQuery.trim(response)+"\" height=\"96\" width=\"128\"></img></div>");
            }
        });
        $('#upload').click(function() {
            $('#uploadify').uploadifyUpload();
            return false;
        });
    });
</script>

<%
    int pid = Integer.parseInt(request.getParameter("pid"));
    //Sy_stem.out.println("image_prod.jsp - pid:" + pid);
    EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
    EntityManager em = emf.createEntityManager();
    Prodotto p = em.find(Prodotto.class, pid);
    em.refresh(p);
    List<File> list = p.getFileList();
%>

<div class="box">
    <div class="title">Image for the Products</div>
    <div class="images">
        <%
            for (File f : list) {
                em.refresh(f);
                if (f.getCopertina()) {
                     out.print("<div id=\"file_" + f.getIdfile() + "\" onclick=\"javascript:preview(" + f.getIdfile() + ","+pid+")\"><img src=\"../GetImageServlet?th=y&fid=" + f.getIdfile() + "\" height=\"96\" width=\"128\"></img><p class=\"img_cover\">COVER</p></div>");
                } else {
                    out.print("<div id=\"file_" + f.getIdfile() + "\" onclick=\"javascript:preview(" + f.getIdfile() + ","+pid+")\"><img src=\"../GetImageServlet?th=y&fid=" + f.getIdfile() + "\" height=\"96\" width=\"128\"></img></div>");
                }
            }
        %>
    </div>
</div>
<div class="box">
    <input id="uploadify" type="file">
    <a id="upload"style="display: none" href="#">Upload</a>
</div>
<div class="box" style="display: none" id="preview">
    <div class="title">Preview Image</div>
    <div id="image"></div>
</div>

<%
    em.close();
    //emf.close();
%>